import React, { useEffect, useState } from 'react';
import { Button, Flex, Toast } from '@ant-design/react-native';
import UserStore from '@/stores/userStore';
import LmoNrcStore from './lmonrcStore';
import NrcCardView from './nrcCard';

// NRC 审批页面
function NrcAuditPage({ route, navigation }) {
  const [nrcData, setNrcData] = useState({});
  const userStore = UserStore.useStore();
  const lmonrcStore = LmoNrcStore.useStore();
  useEffect(() => {
    if (route.params) {
      handleLoad(route.params);
    }
  }, [route.params]);
  const handleLoad = async (nrcItem) => {
    setNrcData(nrcItem);
    // const res = await lmonrcStore.getNrcData(id);
    // if (!res.errorCode) {
    //   setNrcData(res.data);
    // }
  };
  const handleChange = (field, value) => {
    setNrcData({ ...nrcData, [field]: value });
  };
  const handleReject = async () => {
    const res = await lmonrcStore.rejectNrc(userStore.user.LoginName, nrcData);
    if (!res.errorCode) {
      navigation.goBack();
    } else {
      Toast.fail(res.errorMsg);
    }
  };
  const handleAccept = async () => {
    const res = await lmonrcStore.acceptNrc(userStore.user.LoginName, nrcData);
    if (!res.errorCode) {
      navigation.goBack();
    } else {
      Toast.fail(res.errorMsg);
    }
  };
  return (
    <>
      <NrcCardView
        nrc={nrcData}
        onFieldChange={handleChange}
        withAudit={true}
        inAudit={true}
      />
      <Flex>
        <Flex.Item>
          <Button
            type="primary"
            onPress={handleAccept}
            loading={lmonrcStore.acceptNrc.loading}
          >
            同意
          </Button>
        </Flex.Item>
        <Flex.Item>
          <Button
            type="ghost"
            onPress={handleReject}
            loading={lmonrcStore.rejectNrc.loading}
          >
            退回
          </Button>
        </Flex.Item>
      </Flex>
    </>
  );
}

NrcAuditPage.routeName = 'NrcAuditPage';
NrcAuditPage.permission = 'RNrcAudit';
export default NrcAuditPage;
